import java.util.List;

import javax.persistence.Query;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.demo.employee.HibernateUtils;
import com.demo.inheritance.tableperclasshierarcy.ParentLoginDetailsDTO;
import com.demo.service.dao.HibernateDaoService;


public class HQLMain {


	HibernateDaoService daoService=new HibernateDaoService();
	public static void main(String[] args) {


		//hQL 
		//similar to sql 
		//select * from EPLOYEE_MASTER  WHERE  COLUMN, 

		//SELECT * FROM com.domain.hello.EntityName 

		HQLMain hqlMain=new HQLMain();
		//hqlMain.selectValueFromDB();

		//hqlMain.selectValueFromDBUsingDetachedCriteria();
         hqlMain.selectDatafromDbByNamedQuery();

	}

	//second level cache  exp. 

	public void selectValueFromDBUsingDetachedCriteria(){


		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(ParentLoginDetailsDTO.class);
		//detachedCriteria.add(Restrictions.eq(ParentLoginDetailsDTO.USER_ID, 2));
		//detachedCriteria.add(Restrictions.sqlRestriction(sql, value, type))
		ProjectionList projectionList=Projections.projectionList();
		projectionList.add(Projections.count(ParentLoginDetailsDTO.USER_ID));
		//projectionList.add(Projections.)
		detachedCriteria.setProjection(projectionList);
		detachedCriteria.addOrder(Order.desc(ParentLoginDetailsDTO.USER_ID));



		//SELECT COUNT(*) FROM SOMETHING 

		//SELECT COUNT(USER_ID) FROM SOMETHING 

		List<Long> list=(List<Long>)  daoService.loadAllRowsFroDetachedCriteria(detachedCriteria);


		System.out.println(list.get(0));
	} 



	public void selectDatafromDbByNamedQuery(){
		Session session=HibernateUtils.getSessionFactory().openSession();
		session.beginTransaction();
		org.hibernate.Query query=   session.getNamedQuery("ParentLoginDetailsDTO.countRow");
		List<Long> list=	query.list();
		System.out.println(list.size());
		session.getTransaction().commit();
		session.close();

	}




	public void selectValueFromDB(){

		Session session=HibernateUtils.getSessionFactory().openSession();
		session.beginTransaction();

		org.hibernate.Query query= session.createQuery("select fullName,username from com.demo.inheritance.tableperclasshierarcy.ParentLoginDetailsDTO");
		query.setCacheable(true);
		
		//  query.setInteger("parentId", 2);
		
                   /*  SQLQuery query2=session.createSQLQuery("");
                     query2.list();*/
		
		//to execute hql query
		//List<Object[]>  you ll get this if you want selected output. 
		//List<String> list=query.list();

		List<Object[]> list=query.list();
		System.out.println(list.size());


		/*for (String fullName : list) {

			System.out.println(fullName);
		}*/

		for (Object[] objects : list) {

			System.out.println("full Name :"+objects[0]);
			System.out.println("Username :"+objects[1]);


		}



		/* for (ParentLoginDetailsDTO parentLoginDetailsDTO : list) {

			 System.out.println(parentLoginDetailsDTO.getFullName());


		}*/
		session.getTransaction().commit();
		session.close();

	}



}
